package com.budong.service;

import com.budong.component.QiNiuComponent;
import com.budong.service.proxy.GifProxyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static com.budong.common.GifConstant.*;

/**
 * Created by jianweilin on 2017/1/14.
 */
@Service
public class GifService {

    @Autowired
    private GifProxyService gifProxyService;

    @Value("${base.path}")
    private String basePath;

    @Autowired
    private QiNiuComponent qiNiuComponent;

    public List<String> getRealUrl(String videoUrl) {
        return gifProxyService.isYouTUAndAcFunc(videoUrl) ? gifProxyService.getRealVideoUrl(videoUrl, YOU_KU) : gifProxyService.getRealVideoUrl(videoUrl, TECENT);
    }

    public String getSingVideoGif(String mp4Url, String startTime, String endTime,String gifSize) {
        String qiuNiuAddress = "";
        try {
            System.out.println("本地生成GIF开始 ");
            long start = System.currentTimeMillis();
            String gifLocalPath = gifProxyService.getLocalGifPath(mp4Url, startTime, endTime,gifSize);
            System.out.println("本地生成GIF SPEND => " + (System.currentTimeMillis() - start) / (double) 1000 + " 秒");

            if (GIF_MAKE_ERROR.equals(gifLocalPath)) {
                gifProxyService.removeAllGif();
                return GIF_MAKE_ERROR;
            }

            // 压缩图片至宽为500的像素
//            String compress500Gif = basePath + UUID.randomUUID() + ".gif";
//            gifProxyService.compressGifTo500Width(gifLocalPath,compress500Gif);

            // 压缩GIF
            String compressGif = basePath + UUID.randomUUID() + ".gif";
            gifProxyService.compressLocalGif(gifLocalPath,compressGif);

            qiuNiuAddress = qiNiuComponent.uploadFile(compressGif);

            // 删除本地副本
            gifProxyService.deleteLocalGif(Arrays.asList(gifLocalPath,compressGif));
        }catch (Exception e){
            gifProxyService.removeAllGif();
        }
        return qiuNiuAddress;
    }

    public String getMutilVideoGif(String realUrlA, String startTimeA, String endTimeA, String realUrlB, String startTimeB, String endTimeB,String gifSize) {
        String uniteQiNiu = "";
        try {
            String localGifA = gifProxyService.getLocalGifPath(realUrlA, startTimeA, endTimeA, gifSize);
            System.out.println("gifA => " + localGifA);

            String localGifB = gifProxyService.getLocalGifPath(realUrlB, startTimeB, endTimeB, gifSize);
            System.out.println("gifB => " + localGifB);

            if (GIF_MAKE_ERROR.equals(localGifA) || GIF_MAKE_ERROR.equals(localGifB)) {
                gifProxyService.removeAllGif();
                return GIF_MAKE_ERROR;
            }

            String localUniteGifPath = gifProxyService.getLocalUnitGif(localGifA, localGifB);

            if (UNITE_GIF_ERROR.equals(localUniteGifPath)) {
                gifProxyService.removeAllGif();
                return UNITE_GIF_ERROR;
            }

            // 压缩图片至宽为500的像素
//            String compress500Gif = basePath + UUID.randomUUID() + ".gif";
//            gifProxyService.compressGifTo500Width(localUniteGifPath, compress500Gif);

            // 压缩GIF
            String compressGif = basePath + UUID.randomUUID() + ".gif";
            gifProxyService.compressLocalGif(localUniteGifPath, compressGif);

            // 上传七牛云
            uniteQiNiu = qiNiuComponent.uploadFile(compressGif);

            // 删除本地副本
            gifProxyService.deleteLocalGif(Arrays.asList(localGifA, localGifB, localUniteGifPath, compressGif));
        }catch (Exception e){
            gifProxyService.removeAllGif();
        }
        return uniteQiNiu;
    }

}
